package book;

import java.math.BigDecimal;
import java.util.Arrays;

public class BookList {

    private static final int DEFAULT_CAPACITY = 3;

    //储存图书对象的数组
    private Book [] books;

    //书架的大小 有多少本书
    private Integer usedSize;

    //书架的容量
    private Integer capacity;

    public BookList() {
        this.books = new Book[DEFAULT_CAPACITY];
        this.usedSize = 0;
        this.capacity = DEFAULT_CAPACITY;
        loadBooks();
    }

    public void loadBooks() {
        books[0] = new Book("红楼梦","曹雪芹", BigDecimal.valueOf(19.9),"小说");
        books[1] = new Book("水浒传","施耐庵", BigDecimal.valueOf(9.9),"小说");
        usedSize+=2;
    }

    /**
     * 检查是否需要扩容, 如果需要会进行扩容操作
     */
    public void checkCapacity() {
        if(this.usedSize.equals(this.capacity)) {
            //每次扩容到原来的两倍 左移
            this.capacity <<= 1;
            this.books = Arrays.copyOf(books,capacity);
        }
    }

    /**
     * 获取指定数组中指定下标的图书对象
     * @param pos 下标
     * @return 图书对象
     */
    public Book getBook(int pos) {
        return books[pos];
    }

    /**
     * 将图书对象放入数组指定下标处
     * @param pos 位置
     * @param book 图书对象
     */
    public void setBook(int pos, Book book) {
        books[pos] = book;
    }

    /**
     * 获取
     * @return usedSize 书架上书的数量
     */
    public Integer getUsedSize() {
        return usedSize;
    }

    /**
     * 设置
     * @param usedSize 书架上书的数量
     */
    public void setUsedSize(Integer usedSize) {
        this.usedSize = usedSize;
    }

    public String toString() {
        return "BookList{books = " + books + ", usedSize = " + usedSize + "}";
    }
}
